package huixue.cn.com.rxapp.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import huixue.cn.com.rxapp.util.LogUtils;

/**
 * Created by gongdongyang 2019/8/7
 * Describe:
 */
public class MyViewGroup extends ViewGroup {

    Paint paint;

    int width,hieght;

    public MyViewGroup(Context context) {
        this(context,null);

    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.GREEN);
        setWillNotDraw(false);
        setLayerType(LAYER_TYPE_SOFTWARE,paint);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(new RectF(0,0,width,hieght),paint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.hieght = h;
        this.width = w;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        LogUtils.d("MyViewGroup","==onMeasure==");
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        for (int index=0;index<getChildCount();index++){//测量所有的子布局
            View subview = getChildAt(index);
            ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) subview.getLayoutParams();
            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,0, lp.width);
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,0, lp.height);
            subview.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View view = getChildAt(0);//获得主菜单按钮
        view.layout(l,t,r,b);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean flag = super.dispatchTouchEvent(ev);
        //LogUtils.e("MyViewGroup","MyViewGroup==dispatchTouchEvent="+flag);

        return flag;
        //return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        //LogUtils.e("MyViewGroup","MyViewGroup==onTouchEvent="+MotionEvent.actionToString(event.getAction()));
        return super.onTouchEvent(event);
    }
}
